package com.shms.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shms.bean.DataOfIchart;
import com.shms.bean.DataOfLine;
import com.shms.bean.DoubleArray;
import com.shms.bean.GroupOfPoint;
import com.shms.bean.StringArray;
import com.shms.constant.IChartConstant;
import com.shms.constant.QueryConstant;
import com.shms.entity.ShBuild;
import com.shms.entity.ShGrade;
import com.shms.entity.ShHome;
import com.shms.entity.ShPoint;
import com.shms.service.ShPointService;

/**
 * 文件名：shPointAction.java
 * 编写者：习孟冬
 * 创建时间：2013-1-6
 * 简要描述：
 * 根据各种条件，查询卫生成绩，并转化为相应的图表数据类型，并返回
 */
public class ShPointAction extends ActionSupport {

	private static final long serialVersionUID = 8293885698299429925L;
	private GroupOfPoint groupOfPoint;
	private ShPoint shPoint;
	private ShHome shHome;
	private ShPointService shPointService;
	private List<ShBuild> shbList;
	private String message;
	private Integer[] nos;
	
	public String addPoint() {
		//楼号
		ActionContext.getContext().put("buildbno", 0);
		//宿舍号
		ActionContext.getContext().put("homehno", 0);
		return SUCCESS;
	}
	
	/**
	 * 保存成绩信息
	 * @return
	 */
	public String savePoint() {
		boolean rtn = shPointService.savePoint(shPoint);
		if (rtn == false) {
			message = "保存失败";
		} else {
			message = "保存成功";
		}
		//返回楼号
		ActionContext.getContext().put("buildbno", shPoint.getBid());
		//返回宿舍号
		ActionContext.getContext().put("homehno", shPoint.getHid());
		return SUCCESS;
	}
	
	/**
	 * 跳转到宿舍卫生高级查询页面
	 * @return SUCCESS
	 */
	public String advancedQuery() {
		return SUCCESS;
	}
	
	/**
	 * 根据周次和年级统计查询该年级每个宿舍的该周次卫生成绩
	 * @return SUCCESS
	 */
	public String queryByGradeAndWeek() {		
		//查询该年级每个宿舍的该周次卫生成绩
		List<ShPoint> shPoints = shPointService.queryShPoint(shPoint);
		//2D柱状图实体类的List数组
		List<DataOfIchart> dataOfIchart = new ArrayList<DataOfIchart>();
		//将查询结果转化为DataOfIchart图表类型的List数组
		for (int i = 0; i < shPoints.size(); i++) {
			DataOfIchart temp = new DataOfIchart();
			temp.setName(String.valueOf(shPoints.get(i).getPark1()));
			temp.setValue(shPoints.get(i).getPgrade());
			/*
			 * 为柱状图每个数据指定颜色
			 * 该颜色数组是常量数组，也就说长度现在是固定的，
			 * 存在数组越界的风险，使用时注意
			 */
			temp.setColor(IChartConstant.ICHART_COLOR[i]);
			dataOfIchart.add(temp);
		}
		/* 将DataOfIchart2D柱状图表类型的List数组转化为2D柱状图表所需的JSON数据类型
		 * 如:[
		 *   {name : 'IE',value : 35.75,color:'#a5c2d5'},
		 *   {name : 'Chrome',value : 29.84,color:'#cbab4f'}
		 * ];
		 */
		JSONArray jsonData = JSONArray.fromObject(dataOfIchart);
		//这里在request对象中放了一个data，所以struts的result配置中不能有type="redirect"
		//图表数据
		ActionContext.getContext().put("data", jsonData.toString());
		//图表标题
		ActionContext.getContext().put("title", "");
		return SUCCESS;
	}

	/**
	 * 根据宿舍ID和楼ID统计查询该宿舍的卫生成绩
	 * @return SUCCESS
	 */
	public String queryByHidAndBid() {		
		//查询该宿舍的本学期卫生成绩
		List<ShPoint> shPoints = shPointService.queryShPointByBidAndHid(shPoint);
		//折线图数据的实体类
		DataOfLine dataOfLine = new DataOfLine();
		//double类型动态数组
		DoubleArray doubleArray = new DoubleArray();
		StringArray stringArray = new StringArray();
		//取出成绩依次加入到动态数组中
		for (int i = 0; i < shPoints.size(); i++) {
			doubleArray.add(shPoints.get(i).getPgrade());
			stringArray.add("第" + shPoints.get(i).getWeek() + "周");
		}		
		//构造折线图所需数据
		if (shPoints.size() > 0) {
			dataOfLine.setName(shPoints.get(0).getPark1());
			dataOfLine.setValue(doubleArray.getNewDouble());
			//指定折线的颜色
			dataOfLine.setColor(IChartConstant.COLOR_LINE);
			//指定折线的宽度
			dataOfLine.setLine_width(IChartConstant.LINE_WIDTH);
		}
		/*
		 * 将DataOfLine折线图表类型转化为折线图表所需的JSON数据类型
		 * 如:[
		 *   {
         *     name : '北京',
		 *     value:[-9,1,12,20,26,30,32,29,22,12,0,-6],
		 *     color:'#1f7e92',
		 *     line_width:3
		 *   }
		 * ];
		 */
		JSONArray jsonData = JSONArray.fromObject(dataOfLine);
		/*
		 * 将stringArray.getNewString()String数组转化为折线图表X轴坐标所需的JSON数据类型
		 * 如:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
		 */
		JSONArray jsonDataOfX = JSONArray.fromObject(stringArray.getNewString());
		//图表数据
		ActionContext.getContext().put("data", jsonData.toString());
		//图表X轴坐标名
		ActionContext.getContext().put("xstr", jsonDataOfX.toString());
		//图表标题
		ActionContext.getContext().put("title", "");
		return SUCCESS;
	}
	
	/**
	 * 根据年级统计每个宿舍本学期平均成绩
	 * @return SUCCESS
	 */
	public String queryAvgPointByHid() {
		//后期从页面传递过来的
		ShGrade shGrade = new ShGrade();
		shGrade.setGid(9);
		
		//查询每个宿舍本学期平均成绩
		List<ShPoint> shPoints = shPointService.queryAvgPointByHid(shGrade);
		//2D柱状图实体类的List数组
		List<DataOfIchart> dataOfIchart = new ArrayList<DataOfIchart>();
		//将查询结果转化为DataOfIchart图表类型的List数组
		for (int i = 0; i < shPoints.size(); i++) {
			DataOfIchart temp = new DataOfIchart();
			//park1楼名+park2宿舍号，X轴柱状图名
			temp.setName(String.valueOf(shPoints.get(i).getPark1()) + String.valueOf(shPoints.get(i).getPark2()));
			temp.setValue(shPoints.get(i).getPgrade());
			/*
			 * 为柱状图每个数据指定颜色
			 * 该颜色数组是常量数组，也就说长度现在是固定的，
			 * 存在数组越位的风险，使用时注意
			 */
			temp.setColor(IChartConstant.ICHART_COLOR[i]);
			dataOfIchart.add(temp);
		}
		//将DataOfIchart2D柱状图表类型的List数组转化为2D柱状图表所需的JSON数据类型
		JSONArray jsonData = JSONArray.fromObject(dataOfIchart);
		//这里在request对象中放了一个data，所以struts的result配置中不能有type="redirect"
		//图表数据
		ActionContext.getContext().put("data", jsonData.toString());
		//图表标题
		ActionContext.getContext().put("title", "");
		return SUCCESS;
	}
	
	/**
	 * 根据分页信息，查询分数信息列表
	 * @return 
	 */
	public String queryPoint() {
		//获取传来的当前页数
		Integer currentPage = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));
		//获取传来的上一页或下一页标志
		String type = ServletActionContext.getRequest().getParameter("param");
		//构造查询宿舍卫生成绩信息的分页信息输入参数
		GroupOfPoint groupOfPointParams = new GroupOfPoint();
		groupOfPointParams.setCurrentPage(currentPage);
		groupOfPointParams.setType(type);
		groupOfPointParams.setPageSize(QueryConstant.F_Pagesize);
		//查询
		groupOfPoint = shPointService.queryPoint(groupOfPointParams);
		if (groupOfPoint.getShPoint() == null)
			return INPUT;
		else 
			return SUCCESS;
	}
	
	/**
	 * 批量删除成绩信息
	 * @return 
	 */
	public String deletePoint() {
		//获取要删除的单条记录id
		if (ServletActionContext.getRequest().getParameter("no") != null ) {
			Integer id = Integer.parseInt(ServletActionContext.getRequest().getParameter("no"));
			Integer[] ids = {id};
			nos = ids;
		}
		//删除成功重新查询数据，并返回
		if(shPointService.batchDelete(nos)){
			//获取传来的当前页数
			Integer currentPage = groupOfPoint.getCurrentPage() - 1;
			//获取传来的上一页或下一页标志
			String type = groupOfPoint.getType();
			//构造查询宿舍卫生成绩信息的分页信息输入参数
			GroupOfPoint groupOfPointParams = new GroupOfPoint();
			groupOfPointParams.setCurrentPage(currentPage);
			groupOfPointParams.setType(type);
			groupOfPointParams.setPageSize(QueryConstant.F_Pagesize);
			//查询
			groupOfPoint = shPointService.queryPoint(groupOfPointParams);
			return SUCCESS;
		}else{
			message = "删除宿舍卫生成绩信息失败";
			return INPUT;
		}
	}
	
	public String queryByPointId() {
		//获取要查询的单条记录id
		long id = Long.parseLong(ServletActionContext.getRequest().getParameter("no"));
		ShPoint shPointParams = new ShPoint();
		shPointParams.setPid(id);
		shPoint = shPointService.queryById(shPointParams);
		//返回楼号
		ActionContext.getContext().put("buildbno", shPoint.getBid());
		//返回宿舍号
		ActionContext.getContext().put("homehno", shPoint.getHid());
		return SUCCESS;
	}
	
	public GroupOfPoint getGroupOfPoint() {
		return groupOfPoint;
	}

	public void setGroupOfPoint(GroupOfPoint groupOfPoint) {
		this.groupOfPoint = groupOfPoint;
	}

	public List<ShBuild> getShbList() {
		return shbList;
	}

	public void setShbList(List<ShBuild> shbList) {
		this.shbList = shbList;
	}

	public ShPointService getShPointService() {
		return shPointService;
	}

	/**
	 * spring 依赖注入
	 * @param shPointService
	 */
	public void setShPointService(ShPointService shPointService) {
		this.shPointService = shPointService;
	}

	public Integer[] getNos() {
		return nos;
	}

	public void setNos(Integer[] nos) {
		this.nos = nos;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public ShPoint getShPoint() {
		return shPoint;
	}

	public void setShPoint(ShPoint shPoint) {
		this.shPoint = shPoint;
	}

	public ShHome getShHome() {
		return shHome;
	}

	public void setShHome(ShHome shHome) {
		this.shHome = shHome;
	}
	
}
